﻿using Microsoft.Extensions.Logging;
using RBAC.MaHaiBo.Entity;
using RBAC.MaHaiBo.Repository.Interface;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net.Http;
using System.Security.Claims;
using System.Text;
using System.Threading.Tasks;
using Microsoft.AspNetCore;
using System.Security.Cryptography;

namespace RBAC.MaHaiBo.Service
{
    public class LeaveService : ILeaveService
    {
        /// <summary>
        /// 日志
        /// </summary>
        ILogger<LeaveService> logger;

        /// <summary>
        /// 用户仓储
        /// </summary>
        IUserRepository uRep;
        /// <summary>
        /// 请假仓储
        /// </summary>
        ILeaveRepository lRep;
        /// <summary>
        /// 构造函数
        /// </summary>
        /// <param name="logger">日志</param>
        /// <param name="uRep">用户仓储</param>
        /// <param name="lRep">请假仓储</param>
        public LeaveService(ILogger<LeaveService> logger, IUserRepository uRep, ILeaveRepository lRep)
        {
            this.logger = logger;
            this.uRep = uRep;
            this.lRep = lRep;
        }
        /// <summary>
        /// 显示部门领导
        /// </summary>
        /// <param name="department">部门</param>
        /// <returns>返回数据集合</returns>
        public async Task<List<UserInfo>> Getdepartment(int department)
        {
            try
            {
                return await uRep.GetAsync(p => !p.deleteFlag && p.department == department);
            }
            catch (Exception ex)
            {
                logger.LogError("【请假服务层】 显示部门领导" + ex.Message);
                throw;
            }
        }
        /// <summary>
        /// 反填请假人
        /// </summary>
        /// <param name="Uid">用户编号</param>
        /// <returns>返回数据集合</returns>
        public async Task<UserInfo> GetUser(int Uid)
        {
            try
            {
                return await uRep.GetModelAsync(Uid);
            }
            catch (Exception ex)
            {
                logger.LogError("【请假服务层】 反填请假人" + ex.Message);
                throw;
            }
        }
        /// <summary>
        /// 添加请假单
        /// </summary>
        /// <param name="leave">要添加的数量</param>
        /// <returns>返回受影响行数</returns>
        public async Task<int> AddLeave(Leave leave)
        {
            try
            {
                return await lRep.AddAsync(leave);
            }
            catch (Exception ex)
            {
                logger.LogError("【请假服务层】 添加请假单" + ex.Message);
                throw;
            }
        }

        /// <summary>
        /// 分页显示请假列表
        /// </summary>
        /// <param name="poageiden">页码</param>
        /// <param name="pageSzen">页容量</param>
        /// <param name="Name">请假人</param>
        /// <returns>返回数据信息</returns>
        public async Task<FenYeDTO<LeaveDTo>> GetAllLeave(int poageiden, int pageSzen, string? Name, int Uid,int lSdate, DateTime? Addtime)
        {
            try
            {
                var fy = new FenYeDTO<LeaveDTo>();
                var list = (from l in await lRep.GetAllAsync()
                            join u in await uRep.GetAllAsync()
                            on l.BTName equals u.uId
                            where l.IsDeleted == false
                            select new LeaveDTo
                            {
                                Lid = l.Lid,
                                Uid=l.Uid,
                                UName = l.UName,
                                department = l.department,
                                Ltype = l.Ltype,
                                KAddtime = l.KAddtime,
                                JAddtime = l.JAddtime,
                                GZTime = l.GZTime,
                                BdTime = l.BdTime,
                                LSdata = l.LSdata,
                                BTName = l.BTName,
                                UNameLD = u.uName,
                                AddTime = l.AddTime,
                                LSdate = l.LSdate,
                                IsDeleted = l.IsDeleted
                            });
                list = list.Where(p =>p.Uid== Uid||p.BTName== Uid);

                if (!string.IsNullOrEmpty(Name))
                {
                    list = list.Where(p => p.UName.Contains(Name));
                }
                if (lSdate!=0)
                {
                    list = list.Where(p => p.LSdate==(LeaveSdate)lSdate);
                }
                if (Addtime!=null)
                {
                    list = list.Where(p => p.JAddtime>= Addtime);
                }

                fy.totyCount = list.Count();
                fy.pageCount = (int)Math.Ceiling(fy.totyCount * 1.0 / pageSzen);
                fy.list = list.OrderBy(p => p.Lid).Skip((poageiden - 1) * pageSzen).Take(pageSzen).ToList();
                return fy;
            }
            catch (Exception ex)
            {
                logger.LogError("【请假服务层】 分页显示请假列表" + ex.Message);
                throw;
            }
        }

        /// <summary>
        /// 反填申请信息
        /// </summary>
        /// <param name="Lid">申请编号</param>
        /// <returns>返回数据信息</returns>
        public  async Task<Leave> GetLeaveinfo(int Lid)
        {
            try
            {
                return await lRep.GetModelAsync(Lid);
            }
            catch (Exception ex)
            {
                logger.LogError("【请假服务层】 反填申请信息" + ex.Message);
                throw;
            }
        }
        /// <summary>
        /// 修改信息
        /// </summary>
        /// <param name="leave">修改后的数据信息</param>
        /// <returns>返回受影响行数</returns>
        public async Task<int> UPdLeave(Leave leave)
        {
            try
            {
                return await lRep.UpdateAsync(leave);
            }
            catch (Exception ex)
            {
                logger.LogError("【请假服务层】 修改信息" + ex.Message);
                throw;
            }
        }
        /// <summary>
        /// 审计
        /// </summary>
        /// <param name="Lid">申请编号</param>
        /// <returns>返回受影响行数</returns>
       public async Task<int> LeaveSJ(int Lid)
        {
            try
            {
               var qouen=await lRep.GetModelAsync(Lid);
                qouen.LSdate = (LeaveSdate)20;
                return await lRep.UpdateAsync(qouen);
            }
            catch (Exception ex)
            {
                logger.LogError("【请假服务层】 审计" + ex.Message);
                throw;
            }
        }
        /// <summary>
        /// 删除
        /// </summary>
        /// <param name="Lid">申请编号</param>
        /// <returns>返回受影响行数</returns>
       public async Task<int> LeaveDel(int Lid)
        {
            try
            {
                var qouen = await lRep.GetModelAsync(Lid);
                qouen.IsDeleted = true;
                return await lRep.UpdateAsync(qouen);
            }
            catch (Exception ex)
            {
                logger.LogError("【请假服务层】 删除" + ex.Message);
                throw;
            }
       }

    }
}
